package com.zzf.concurrent.example.sync;

import com.zzf.concurrent.example.sync.bean.TicketRunnable;
import com.zzf.concurrent.example.sync.bean.TicketRunnable2;
import com.zzf.concurrent.example.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author GaoFeng2017
 * @date 2019/8/6 7:59
 */
@Slf4j
public class SynchronizedExample1 {

    public static void main(String[] args) {
        // objectLock();
        // thisLock();
        classLock();
    }

    private static void objectLock() {
        TicketRunnable ticketRunnable = new TicketRunnable();

        Thread t1 = new Thread(ticketRunnable);
        Thread t2 = new Thread(ticketRunnable);
        Thread t3 = new Thread(ticketRunnable);

        t1.start();
        t2.start();
        t3.start();
    }

    private static void thisLock() {
        TicketRunnable2 ticketRunnable2 = new TicketRunnable2();
        ticketRunnable2.test();
    }

    private static void classLock() {

        Thread t1 = new Thread() {
            @Override
            public void run() {
                t1();
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                t2();
            }
        };

        t1.start();
        t2.start();
    }

    public synchronized static void t1() {
        log.info("{} is start", Thread.currentThread().getName());
        TimeUtil.sleep(300000);
    }

    public synchronized static void t2() {
        log.info("{} is start", Thread.currentThread().getName());
    }
}
